import { routerRedux } from 'dva/router'
import * as serviceLogin from './../services/login';

export default {
    namespace: 'login',
    state: {},
    effects: {
        * login ({ payload }, { put, call ,select }) {
            const { values:{ username } } = payload;
            const data = yield call(serviceLogin.login, payload);
            const { locationPathname } = yield select(_ => _.dashboard);
            window.localStorage.setItem('taken', username);
            if (data.success) {
                yield put({ type: 'dashboard/query' });
                const { from } = locationPathname;
                if (from && from !== '/login'){
                    yield put(routerRedux.push(from));
                } else {
                    yield put(routerRedux.push('/home'));
                }
            } else {
                throw data
            }
        }
    }
}
